#cmake file for project zlib
#author:guyadong
#created:2016/03/29
cmake_minimum_required( VERSION 2.6 )
#设置处理器
if(TARGET_PROCESSOR)
	set (CMAKE_SYSTEM_PROCESSOR ${TARGET_PROCESSOR})
endif()
## 对给定的library_name,在lib_dir中查找import library，
## 定义为{library_name}(大写)_LIBRARY_IMPORT全局变量
function(defineImportLibraryVariable library_name lib_dir)
	string(TOUPPER ${library_name} var_prefix)
	##查找${library_name}的动态导入库(import library)
	unset(${var_prefix}_LIBRARY_IMPORT CACHE)
	find_library( ${var_prefix}_LIBRARY_IMPORT
		NAMES ${library_name}
		PATHS "${lib_dir}/lib"
		DOC "${var_prefix} import library"
		NO_DEFAULT_PATH
	)
	message(STATUS "${var_prefix}_LIBRARY_IMPORT=${${var_prefix}_LIBRARY_IMPORT}" )
endfunction()

## 对给定的library_name,在lib_dir中查找static library，
## 定义为{library_name}(大写)_LIBRARY_STATIC全局变量
function(defineStaticLibraryVariable library_name lib_dir)
	string(TOUPPER ${library_name} var_prefix)
	##计算${library_name}的静态库名
	set ( static_library_name ${library_name}static${CMAKE_STATIC_LIBRARY_SUFFIX})
	##查找${library_name}的静态库
	unset(${var_prefix}_LIBRARY_STATIC CACHE)
	find_library( ${var_prefix}_LIBRARY_STATIC
		NAMES ${static_library_name}
		PATHS "${lib_dir}/lib"
		DOC "${library_name} static library"
		NO_DEFAULT_PATH
	)
	message(STATUS "${var_prefix}_LIBRARY_STATIC=${${var_prefix}_LIBRARY_STATIC}" )
endfunction()
cmake_policy(SET CMP0054 NEW) 
# gcc已经提供了zlib支持，不需要定义
if ( MSVC)
if ( WIN32 AND CMAKE_SYSTEM_PROCESSOR MATCHES  "AMD64|x86_64")
	set ( zlib_flolder "${CMAKE_CURRENT_LIST_DIR}/zlib-windows-vc-x86_64" )
elseif(WIN32 AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
	set ( zlib_flolder "${CMAKE_CURRENT_LIST_DIR}/zlib-windows-vc-x86" )
else()
	message(FATAL_ERROR "INVALID CMAKE_SYSTEM_PROCESSOR:${CMAKE_SYSTEM_PROCESSOR}") 
endif()

defineImportLibraryVariable(zlib ${zlib_flolder})
defineStaticLibraryVariable(zlib ${zlib_flolder})
set(INCLUDE_ZLIB "${zlib_flolder}/include" CACHE INTERNAL "zlib include" )
message(STATUS "INCLUDE_ZLIB=${INCLUDE_ZLIB}")
endif()
